package com.google.sydym6.controllers;

import java.io.IOException;

import com.google.sydym6.logic.RadioScrobblerModel;
import com.google.sydym6.logic.managers.IRadioManager;
import com.google.sydym6.logic.managers.ISongManager;
import com.google.sydym6.view.activities.AbstractMiniPlayerActivity;

public class PlayRadioController implements IPlayRadioController {

	
	AbstractMiniPlayerActivity view;
	IRadioManager radioManager;
	ISongManager songManager;
	
	public PlayRadioController(AbstractMiniPlayerActivity view) {
		this.view = view;
		radioManager = RadioScrobblerModel.getInstance().getRadioManager();
		songManager = RadioScrobblerModel.getInstance().getSongManager();
	}
	
	@Override
	public void playStopRadio() {
		new Thread() {
			
			@Override	
			public void run() {
				if (radioManager.isPlaying()) {
					radioManager.stopRadio();
				} else {
					
					if (songManager.isPlaying())
						songManager.stopSong();
					songManager.release();
					try {
						radioManager.playRadio();
					} catch (IllegalArgumentException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (SecurityException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IllegalStateException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			
		}.start();
	}
}
